// pages/index/reservation /reservation .js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
   
    scrolls1:[
      
    ],
    scrolls2:[
      
    ],
    day:'',//日期
    hour_time:'',//时间段
    house:'',//房间类型
    long:'',//几小时
    currentIndex:0,
    currentInde:0,
    current:0,
    active:false,
    times:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    // 头部请求数据
    app.wxrequest({
      url: app.globalData.baseUrl +'/api/house/index',
      data:{
        session_key:app.gs()
      },
      method:'GET',
      success:function(res){
        console.log('resres',res)
        wx.hideLoading()
        that.setData({
            top:res.data.data,
            shopname:options.shopname
        })
      }
    })
  
    var scrolls1 = [];
    var scrolls = [];
    var that = this;
    var day = new Date().getTime();
    for(var i = 0;i <9;i++){
      scrolls1.push({});
      scrolls.push(day + 86400000 * i);
    }
    for(var i=0;i < 9;i++){
      scrolls1[i].date = new Date(scrolls[i]).getDate();//日期
      scrolls1[i].month = new Date(scrolls[i]).getMonth();//月份
      scrolls1[i].time = new Date(scrolls[i]).getDay();//星期几
      scrolls1[i].year = new Date(scrolls[i]).getFullYear();//年
    }
  
    
    for(var i=0;i<9;i++){
      scrolls1[i].month = scrolls1[i].month + 1;
      
      switch (scrolls1[i].time) {
        case 0:
          scrolls1[i].time = '星期日';
          break;
        case 1:
          scrolls1[i].time = '星期一';
          break;
        case 2:
          scrolls1[i].time = '星期二';
          break;
        case 3:
          scrolls1[i].time = '星期三';
          break;
        case 4:
          scrolls1[i].time = '星期四';
          break;
        case 5:
          scrolls1[i].time = '星期五';
          break;
        case 6:
          scrolls1[i].time = '星期六';
          break;
      }
      scrolls1[0].time ="今天";
      scrolls1[1].time = "明天";
      scrolls1[2].time = "后天";
    }
    that.setData({
      scrolls1: scrolls1,
      day: scrolls1[0].time + '(' + scrolls1[0].month + '-' + scrolls1[0].date+')'
    })
    app.wxrequest({
      url: app.globalData.baseUrl + '/api/house/house',
      method: 'GET',
      data: {
        session_key: app.gs(),
        appoint_date: that.data.scrolls1[that.data.currentIndex].year + '-' + that.data.scrolls1[that.data.currentIndex].month + '-' + that.data.scrolls1[that.data.currentIndex].date
      },
      success: function (res) {
        wx.hideLoading();
        that.setData({
          houseCate: res.data.data.houseCate,
          scrolls2: res.data.data.houseCate[0].house,
          house: res.data.data.houseCate[0].house_cate,
          num: res.data.data.houseCate[0].num,
          year: that.data.scrolls1[that.data.currentIndex].year,
          day2: that.data.scrolls1[that.data.currentIndex].month + '-' + that.data.scrolls1[that.data.currentIndex].date,
         
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  hitMouse1(e){
    var that = this;
    // console.log(e)
      this.setData({
        currentIndex:e.currentTarget.dataset.index
      }, function () {
        app.wxrequest({
          url: app.globalData.baseUrl + '/api/house/house',
          method: 'GET',
          data: {
            session_key: app.gs(),
            appoint_date: that.data.scrolls1[that.data.currentIndex].year + '-' + that.data.scrolls1[that.data.currentIndex].month + '-' + that.data.scrolls1[that.data.currentIndex].date
          },
          success: function (res) {
            wx.hideLoading()
            
            console.log(res);
            // console.log('month',that.data.scrolls1[that.data.currentIndex].month)
            // console.log('date',that.data.scrolls1[that.data.currentIndex].date)
            if(1){}
            that.setData({
              houseCate: res.data.data.houseCate,
              scrolls2:res.data.data.houseCate[0].house,
              currentInde:0,
              day: that.data.scrolls1[that.data.currentIndex].time + '(' + that.data.scrolls1[that.data.currentIndex].month + '-' + that.data.scrolls1[that.data.currentIndex].date + ')',
              year: that.data.scrolls1[that.data.currentIndex].year,
              day2: that.data.scrolls1[that.data.currentIndex].month + '-' + that.data.scrolls1[that.data.currentIndex].date
              // house_cate_id: res.data.data.houseCate[0].house[that.data.currentInde].id
            })
          }
        })
      })
  },
  hitMouse2(e) {
    // console.log(e)
    var that = this;
    this.setData({
      currentInde: e.currentTarget.dataset.inde,
      scrolls2: that.data.houseCate[e.currentTarget.dataset.inde].house,
      house: that.data.houseCate[e.currentTarget.dataset.inde].house_cate,
      num: that.data.houseCate[e.currentTarget.dataset.inde].num
    })
  },
  hitMouse(e) {
    // console.log(e)
    this.setData({
      current: e.currentTarget.dataset.index,
      startt:e.currentTarget.dataset.startt
    })
  },
  appointment(e){
    // console.log(e)
    let that = this;
    let time = new Date();
    let hour = time.getHours();
    let minute = time.getMinutes();
    let H,S1,S2,S3;
   
    if (parseInt(e.currentTarget.dataset.hou) > parseInt(e.currentTarget.dataset.hour)){
      // 不跨天的时间

       H = (parseInt(e.currentTarget.dataset.hou) - parseInt(e.currentTarget.dataset.hour)) / 0.5 - 1;
      //  H = (parseInt(e.currentTarget.dataset.hou) - parseInt(e.currentTarget.dataset.hour) - parseInt(e.currentTarget.dataset.long)) / 0.5 - 1;
       console.log(H);
       S1 = e.currentTarget.dataset.hour.split(':');
       S2 = parseInt(S1[0] * 60) + parseInt(S1[1]);
       S3 = []; 

      for (var i = 0; i < H; i++) {
        if ((S2 + 30 * i) % 60 == 0) {
          if ((S2 + 30 * i) >= (hour * 60 + minute + 15)) {
            S3.push((parseInt((S2 + 30 * i) / 60) % 24 + ':00'))
          }
          else {
            continue
          }
        } else {
          if ((S2 + 30 * i) >= (hour * 60 + minute + 15)) {
            S3.push((parseInt((S2 + 30 * i) / 60) % 24 + ':' + (S2 + 30 * i) % 60))
          }
          else {
            continue
          }
        }
      }
    } else if (parseInt(e.currentTarget.dataset.hou) < parseInt(e.currentTarget.dataset.hour)){
      // 跨天时间
      // H = (parseInt(e.currentTarget.dataset.hou) + 24- parseInt(e.currentTarget.dataset.hour) - parseInt(e.currentTarget.dataset.long)) / 0.5;
      H = (parseInt(e.currentTarget.dataset.hou) + 24 - parseInt(e.currentTarget.dataset.hour)) / 0.5-1;
      // console.log(H)
      S1 = e.currentTarget.dataset.hour.split(':');
      S2 = parseInt(S1[0] * 60) + parseInt(S1[1]);
      S3 = []; 
      for (var i = 0; i < H; i++) {
        if ((S2 + 30 * i) % 60 == 0) {
          if ((S2 + 30 * i) >= (hour * 60 + minute + 15)) {
            S3.push((parseInt((S2 + 30 * i) / 60) % 24 + ':00'))
          }
          else {
            continue
          }
        } else {
          if ((S2 + 30 * i) >= (hour * 60 + minute + 15)) {
            S3.push((parseInt((S2 + 30 * i) / 60) % 24 + ':' + (S2 + 30 * i) % 60))
          }
          else {
            continue
          }
        }
      }
    }
   
    that.setData({
      active:true,
      hour: e.currentTarget.dataset.hour,
      hou:e.currentTarget.dataset.hou,
      price: e.currentTarget.dataset.price,
      long: e.currentTarget.dataset.long,
      times:S3,
      num: e.currentTarget.dataset.num,
      startt: that.data.scrolls2[0].start_time,
       // startt: that.data.scrolls2[that.data.currentIndex].start_time,
      house_cate_id:e.currentTarget.id,
      mobile: e.currentTarget.dataset.mobile
    })
  },
 
  choose(e){
    var _this = this;
        wx.navigateTo({
          url: 'reserve/reserve?day=' + _this.data.day + '&hour=' + _this.data.hour + '&hou=' + _this.data.hou + '&price=' + _this.data.price + '&house=' + _this.data.house + '&long=' + _this.data.long + '&num=' + _this.data.num + '&year=' + _this.data.year + '&day2=' + _this.data.day2 + '&begin_time=' + _this.data.startt + '&house_cate_id=' + _this.data.house_cate_id + '&mobile=' + _this.data.mobile+'&coupon=0'+'&shopname='+_this.data.shopname,
        })
        _this.setData({
          active:false
        })
  },
  hide(){
    this.setData({
      active: false
    })
  },
  areaLocation(){
    var _this = this;
    wx.openLocation({
      latitude: parseInt(_this.data.top.shop.latitude),
      longitude: parseInt(_this.data.top.shop.longitude)
    })
  },
  callPhone(){
    var _this = this;
    wx.makePhoneCall({
      phoneNumber: _this.data.top.shop.photo,
    })
  }
})